package com.tianye.salon.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.tianye.salon.util.CheckUtil;

/**
 * 
 * @ClassName: LoginInterceptor 
 * @Description: 前端用户登录拦截 
 * @author fengchao
 * @date 2017年8月7日 下午2:27:16    
 */
public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
	private final Logger logger = LoggerFactory.getLogger(AdminLoginInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String requestUri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestUri.substring(contextPath.length());
		logger.info("应用上下文：" + contextPath);
		logger.info("请求地址：" + requestUri);
		logger.info("请求路径：" + url);
		
		HttpSession session = request.getSession();
	    Integer adminuid = (Integer) session.getAttribute("adminuid");
		if(CheckUtil.isEmpty(adminuid)){
			//去登陆
			request.getRequestDispatcher("/admin-login/login").forward(request, response);
			return false;
		}
		return true;
	}
}
